package controller;

import com.alibaba.dubbo.config.annotation.Reference;
import entity.Users;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import service.UserService;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @BelongsProject: lagou-dubbo
 * @Author: GuoAn.Sun
 * @CreateTime: 2020-07-27 18:16
 * @Description: 控制层
 */
@Controller
public class UserAction {

    @Reference
    private UserService userService;

    @RequestMapping("/register")
    public String register(Users user) {
        System.out.println("来注册");
        try {
            String nowTime = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
            user.setCreatetime(nowTime);
            userService.register(user);
            return "注册成功！";
        } catch (Exception e) {
            e.printStackTrace();
            return "失败";
        }
    }


    @RequestMapping("/delete")
    public String delete(HttpServletRequest request, HttpServletResponse respons){
        String username= (String) request.getParameter("username");
        System.out.println(username);
        int delete = userService.delete(username);
        if(delete==1){
            return "删除成功！";
        }else {
            return "删除失败！";
        }
    }

    @RequestMapping("/update")
    public String delete(Users users){
        int update = userService.update(users);
        if(update==1){
            return "更新成功！";
        }else {
            return "更新失败！";
        }
    }

    @RequestMapping("/find")
    public void find(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username= (String) request.getParameter("username");
        Users users = userService.find(username);
        System.out.println("list"+users);
        request.getSession().setAttribute("list", users);
    }

}
